import java.io.File;
import java.io.IOException;
import java.util.Scanner;

//实现地柜便利文件,并询问删除
public class Demo3 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的路径:");
        String  s = scanner.next();
        File file = new File(s);
        //判断文件是否存在
        if(!file.exists()) {
            System.out.println("扫描的路径文件不存在!");
            return;
        }
        System.out.println("请输入要删除的文件!");
        String delete = scanner.next();
        toDelete(file,delete);
    }

    public static void toDelete(File file,String str) {
        File[] files = file.listFiles();
        if(files == null) {
            System.out.println("空目录!");
            return;
        }
        //判断文件目录是否为空
        for (File f : files) {
            if(f.isDirectory()) {
                toDelete(f, str);
            }else {
                tryDelete(f,str);
            }
        }
    }

    public static void tryDelete(File f,String str) {
        if(f.getName().contains(str)) {
            try {
                System.out.println("是否要删除" + f.getCanonicalPath());
                Scanner scanner = new Scanner(System.in);
                System.out.println("确认请输入 => Y/N");
                String s = scanner.next();
                if(s.equals("Y")) {
                    f.delete();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
